package db

import (
	"encoding/json"
)

type DbServerConfig struct {
	Id               uint32   `gorm:"column:id;type:int(11)"`           // id
	StopServer       bool     `gorm:"column:stop_server;type:smallint"` // 是否停服
	IpWhiteListJson  string   `gorm:"column:ip_white_list;type:text"`   // ip白名单
	UidWhiteListJson string   `gorm:"column:uid_white_list;type:text"`  // uid白名单
	IpWhiteList      []string `gorm:"-"`
	UidWhiteList     []uint32 `gorm:"-"`
}

func (sc *DbServerConfig) TableName() string {
	return "gm_server_config"
}

func GetServerConfig() (*DbServerConfig, error) {
	serverConfig := new(DbServerConfig)
	err := Conn.Where("id = ?", 1).First(&serverConfig).Error
	if err != nil {
		return nil, err
	}
	err = json.Unmarshal([]byte(serverConfig.IpWhiteListJson), &(serverConfig.IpWhiteList))
	if err != nil {
		return nil, err
	}
	err = json.Unmarshal([]byte(serverConfig.UidWhiteListJson), &(serverConfig.UidWhiteList))
	if err != nil {
		return nil, err
	}
	return serverConfig, nil
}
